package mvc.struts.annotation.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.ObjectMapper;

public final class JsonUtil {

	private	static final	Logger LOGGER = LoggerFactory.getLogger(JsonUtil.class);
	
	private	static final 	ObjectMapper OBJECT_MAPPER = new ObjectMapper();
	
	/**
	 * 
	 * @param obj
	 * @return
	 */
	public static <T> String toJson(T obj)
	{
		String	json;
		
		try
		{
			json = OBJECT_MAPPER.writeValueAsString(obj);
		}
		catch(Exception ex)
		{
			LOGGER.error("convert POJO to JSON failure", ex);
			throw	new	RuntimeException(ex);
		}
		
		return	json;
	}
	
	/**
	 * 
	 * @param json
	 * @param type
	 * @return
	 */
	public static <T> T fromJson(String json, Class<T> type)
	{
		T	pojo;
		
		try
		{
			pojo = OBJECT_MAPPER.readValue(json, type);
		}
		catch(Exception ex)
		{
			LOGGER.error("convert JSON to POJO failure", ex);
			throw	new	RuntimeException(ex);
		}
		
		return	pojo;
	}
}
